package com.inter.law.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.inter.law.dao.IlTaskDAO;
import com.inter.law.entity.IlTask;
import com.inter.law.entity.TaskJson;
import com.inter.law.utils.IlTaskToTaskJsonUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GetTaskJson extends ActionSupport{
    private ApplicationContext ctx = new ClassPathXmlApplicationContext(
			"applicationContext.xml");
	 Map<String, Object> session = ActionContext.getContext().getSession();
	 private String json;
	 private Integer intPage=1;//当前页数
	 private Integer pageSize=5;//每页显示的最大个数
	 private Integer totalNumber;//总页数
	 /**
	  * 获取用户的Task，按照Task开始时间降序排列
	  */
	 @Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//获取当前用户的id
		Integer lawerId=(Integer) session.get("id");
		IlTaskDAO ilTaskDAO=IlTaskDAO.getFromApplicationContext(ctx);
		List<IlTask> list=ilTaskDAO.findByLawerIdOrderByStartTime(lawerId);
		if(list!=null&&list.size()>0){
			Gson gson=new Gson();
			List<TaskJson> list2=IlTaskToTaskJsonUtils.getTaskJsonList(list);
			json=gson.toJson(list2);
		}
		return SUCCESS;
	}
	 /**
	  * 获取用户的Task，按照Task开始时间降序排列，并且进行分页查询
	  */
	 public String getTask() throws Exception {
			// TODO Auto-generated method stub
		    //获取当前用户的id
			Integer lawerId=(Integer) session.get("id");
			IlTaskDAO ilTaskDAO=IlTaskDAO.getFromApplicationContext(ctx);
			//当totalNumber为空或者0的时候去数据库中查找总记录数，并通过总记录数得到总页数
			if(totalNumber==null||totalNumber==0){
				if(ilTaskDAO.findByLawerIdTotal(lawerId)%pageSize==0){
					totalNumber=ilTaskDAO.findByLawerIdTotal(lawerId)/pageSize;
				}else{
					totalNumber=ilTaskDAO.findByLawerIdTotal(lawerId)/pageSize+1;
				}
				
			}

			List<IlTask> list=ilTaskDAO.findByLawerIdOrderByStartTime(lawerId,intPage,pageSize);
			if(list!=null&&list.size()>0){
				Gson gson=new Gson();
				List<TaskJson> list2=IlTaskToTaskJsonUtils.getTaskJsonList(list);
				json=gson.toJson(list2);	
			}
			//将查询到的所有Task与totalNumber都放入json数组中，
			JSONArray jsonArray=JSONArray.fromObject(json);
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("totalNumber", totalNumber);
			jsonArray.add(0, jsonObject);
			json=jsonArray.toString();
			return SUCCESS;
		}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public Integer getIntPage() {
		return intPage;
	}
	public void setIntPage(Integer intPage) {
		this.intPage = intPage;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalNumber() {
		return totalNumber;
	}
	public void setTotalNumber(Integer totalNumber) {
		this.totalNumber = totalNumber;
	}

}
